package com.le.tester.imooc.ccmouse.recursion;

import com.le.tester.imooc.ccmouse.common.Node;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 描述：链表创建(递归的写法，定义了一个定值，data.get(0))
 */
public class LinkedListCreator {

    /**
     * creates a linked list
     *
     * @param data linked list data
     * @return linked list
     */
    public Node createLinkedList(List<Integer> data) {
        if (data.isEmpty()) {
            return null;
        }
        //也是以静制动，定第一个Node，还是会使用基准值的方法
        Node firstNode = new Node(data.get(0));
        //总是一个一个的取出值，会一直不停的循环着
        //让方法本身调用自己，进行递归
        firstNode.setNext(createLinkedList(data.subList(1, data.size())));
        return firstNode;
    }

    public Node createLargeLinkedList(int size) {
        Node prev = null;
        Node head = null;
        for (int i = 1; i <= size; i++) {
            Node node = new Node(i);
            if (prev != null) {
                prev.setNext(node);
            } else {
                head = node;
            }
            prev = node;
        }
        return head;
    }

    public static void main(String[] args) {
        LinkedListCreator linkedListCreator = new LinkedListCreator();
        Node.printLinkedList(
                linkedListCreator.createLinkedList(new ArrayList<>()));

        Node.printLinkedList(
                linkedListCreator.createLinkedList(Collections.singletonList(1)));

        Node.printLinkedList(
                linkedListCreator.createLinkedList(Arrays.asList(1, 2, 3, 4, 5)));
    }
}
